package com.labichaoka.chaoka.ui.bank.display;

import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.BankCardListResponse;
import com.labichaoka.chaoka.entity.IsOpenAcountRequest;
import com.labichaoka.chaoka.entity.IsOpenAcountResponse;
import com.labichaoka.chaoka.net.BaseSubscriber;
import com.labichaoka.chaoka.net.DataManager;
import com.labichaoka.chaoka.ui.fetch.FetchMoneyInteractor;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ruan on 2018/8/8.
 */

public class BankCardDisplayInteractorImpl implements BankCardDisplayInteractor {
    private DataManager manager = new DataManager(MyApplication.mContext);
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();
    @Override
    public void getBankList(OnFinishedListener listFinishedListener) {
        mCompositeSubscription.add(manager.getBankList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BankCardListResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listFinishedListener.onQueryListFailed();
                    }

                    @Override
                    public void onNext(BankCardListResponse response) {
                        super.onNext(response);
                        listFinishedListener.onQueryListSuccessed(response);
                    }
                })
        );
    }

    @Override
    public void isOpenAcount(IsOpenAcountRequest request,OnFinishedListener listener) {
        mCompositeSubscription.add(manager.isOpenAcount(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<IsOpenAcountResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onIsOpenAcountFailed();
                    }

                    @Override
                    public void onNext(IsOpenAcountResponse response) {
                        super.onNext(response);
                        listener.onIsOpenAcountSucc(response);
                    }
                })
        );
    }
}
